<?php

/**
 * ECSHOP 分销明细
 * $Author: wuxuye $
 * $2015-09-07$
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

/*------------------------------------------------------ */
//-- 明细列表
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'detailed')
{   

    /* 检查权限 */
    admin_priv('distributor');
    
    $result = get_user_list();
    
    $smarty->assign('filter', $result['filter']);
    $smarty->assign('result_user', $result['list']);
    $smarty->assign('ur_here', $_LANG['distributor']);
    $smarty->assign('record_count', $result['filter']['record_count']);
    $smarty->assign('page_count', $result['filter']['page_count']);
    $smarty->assign('full_page', 1);
    
    /* 显示模板 */
    assign_query_info();
    $smarty->display('user_detailed.htm');
    
}
/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    /* 检查权限 */
    admin_priv('distributor');
    $result = get_user_list();
    $smarty->assign('filter', $result['filter']);
    $smarty->assign('result_user', $result['list']);
    $smarty->assign('record_count', $result['filter']['record_count']);
    $smarty->assign('page_count', $result['filter']['page_count']);

    make_json_result($smarty->fetch('user_detailed.htm'), '', array('filter' => $result['filter'], 'page_count' => $result['filter']['page_count']));
}

elseif ($_REQUEST['act'] == 'do_withdraw'){
    //执行提现操作    
    if(!empty($_REQUEST['user_id'])){
        //获取这个用户的 下级分销商
        $user_list = array();
        $sql = 'SELECT user_id
            FROM ' . $ecs->table('users') . '
            WHERE parent_uid = '.$_REQUEST['user_id'] ." and agent_rank = 2";
        $user_list = $db->getAll($sql);
        //讲这些用户符合条件的提现订单改为 已提现
        
        $total_price = 0; //提现总金额
        foreach ($user_list as $key => $val){
            $order_list = array();
            $sql = "select order_id 
                    from ".$ecs->table('order_info')."
                    where withdraw_status = 1";
            $order_list = $db->getAll($sql);
            foreach ($order_list as $k => $v){
                $sql = "UPDATE ". $ecs->table('order_info') . " SET withdraw_status = 2 WHERE order_id = ".$v['order_id'];
                $db->query($sql);
                $sql_price = "SELECT goods_amount  ,commissions_ratio FROM " . $GLOBALS['ecs']->table('order_info') . 
                "WHERE order_id = ".$v['order_id'];
                $price = $GLOBALS['db']->getRow($sql_price);
                $total_price += round($price['goods_amount'] * $price['commissions_ratio'],2);
                $sql = "INSERT INTO " .$ecs->table('withdraw_log'). " (order_id, user_id, do_type, status,remark,inputtime)" .
                    "VALUES ('$v[order_id]','$_SESSION[admin_id]',1,2, '管理员在前台操作，完成提现', ".time().")";
                $db->query($sql);
            }
        }
        
        sys_msg("操作成功，共计：".$total_price.'元');
        
    }else{
        die('用户不存在');
    }
}

elseif ($_REQUEST['act'] == 'commission_detailed'){
    
    if(isset($_REQUEST['status']) && !empty($_REQUEST['user_id'])){
        //获取列表
        $list = get_commission_detailed();
       
        $smarty->assign('filter', $list['filter']);
        $smarty->assign('list', $list['list']);
        $smarty->assign('ur_here', $_LANG['distributor']);
        $smarty->assign('record_count', $result['filter']['record_count']);
        $smarty->assign('page_count', $result['filter']['page_count']);
        $smarty->assign('full_page', 1);
        $smarty->assign('status', $result['status']);
        $smarty->assign('user_id', $result['user_id']);
        
        $smarty->display('commission_detailed.htm');
        
    }else{
        die('参数缺失');
    }
    
}
elseif ($_REQUEST['act'] == 'query1')
{

    $result = get_commission_detailed();
    
    $smarty->assign('filter', $result['filter']);
    $smarty->assign('list', $result['list']);
    $smarty->assign('record_count', $result['filter']['record_count']);
    $smarty->assign('page_count', $result['filter']['page_count']);

    make_json_result($smarty->fetch('commission_detailed.htm'), '', array('filter' => $result['filter'], 'page_count' => $result['filter']['page_count']));
}



function get_user_list(){
    
    $filter = array();
    $filter['page'] = empty($_REQUEST['page']) || (intval($_REQUEST['page']) <= 0) ? 1 : intval($_REQUEST['page']);
    if (isset($_REQUEST['page_size']) && intval($_REQUEST['page_size']) > 0)
    {
        $filter['page_size'] = intval($_REQUEST['page_size']);
    }
    elseif (isset($_COOKIE['ECSCP']['page_size']) && intval($_COOKIE['ECSCP']['page_size']) > 0)
    {
        $filter['page_size'] = intval($_COOKIE['ECSCP']['page_size']);
    }
    else
    {
        $filter['page_size'] = 15;
    }
    
    
    $result_user = array();
    
    $start = ($filter['page'] - 1) * $filter['page_size'];
    
    $where = "";
    if(!empty($_REQUEST['user_name'])){
        $where .= " and user_name like '%".$_REQUEST['user_name']."%' ";
    }
    if(!empty($_REQUEST['mobile_phone'])){
        $where .= " and mobile_phone like '%".$_REQUEST['mobile_phone']."%' ";
    }
    
    //拿到一级分销商用户列表
    $user_list = array();
    $sql = 'SELECT user_id,user_name,mobile_phone,agent1_time,withdraw_account
            FROM ' . $GLOBALS['ecs']->table('users') . '
            WHERE agent_rank = 1 '.$where.'
            order by user_id DESC
            limit '.$start.','.$filter['page_size'];
    $user_list = $GLOBALS['db']->getAll($sql);
    
    //逐个获取详细信息
    foreach ($user_list as $key => $val){
        $temp = array();
        //获取所有2级分销商的相关信息
        $two_result = array();
        $sql = "select user_id,agent2_time from ". $GLOBALS['ecs']->table('users') ." where parent_uid = ".$val['user_id']." and agent_rank = 2";
        $two_result = $GLOBALS['db']->getAll($sql);
        $temp['all_user'] = count($two_result);//二级分销商总数
        $temp['all_price'] = 0;//二级分销商的总消费
        $temp['price_0'] = 0;//未结算提成
        $temp['price_1'] = 0;//待结算提成
        $temp['price_2'] = 0;//已结算提成
    
        foreach ($two_result as $two_key => $two_val){
            //拿到订单
            $order_result = array();
            $sql = "select goods_amount,withdraw_status,commissions_ratio
                    from ". $GLOBALS['ecs']->table('order_info') ."
                    where user_id = ".$two_val['user_id']." and add_time >= ".$two_val['agent2_time']." and order_status=5 and shipping_status=2 and pay_status = 2 ";
            $order_result = $GLOBALS['db']->getAll($sql);
            //做统计
            foreach ($order_result as $k => $v){
    
                $temp['all_price'] += $v['goods_amount'];
    
                switch ($v['withdraw_status']){
                    case 0:  //未结算的提成
                        $temp['price_0'] += round($v['goods_amount']*COMMISSIONS_RATIO,2);
                        break;
                    case 1:  //待结算的提成
                        $temp['price_1'] += round($v['goods_amount']*$v['commissions_ratio'],2);
                        break;
                    case 2:  //已结算的提成
                        $temp['price_2'] += round($v['goods_amount']*$v['commissions_ratio'],2);
                        break;
                }
            }
        }
        $temp['user_id'] = $val['user_id'];
        $temp['user_name'] = $val['user_name'];
        $temp['mobile_phone'] = $val['mobile_phone'];
        $temp['agent1_time'] = empty($val['agent1_time'])?"-":date("Y-m-d H:i:s",$val['agent1_time']);
        $temp['withdraw_account'] = $val['withdraw_account'];
        $result_user[] = $temp;
    }
    
    //再拿一遍总数
    $user_count = array();
    $sql = 'SELECT user_id
            FROM ' . $GLOBALS['ecs']->table('users') . '
            WHERE agent_rank = 1 '.$where;
    $user_count = $GLOBALS['db']->getAll($sql);
    
    $filter['user_name'] = empty($_REQUEST['user_name']) ? '' : trim($_REQUEST['user_name']);
    $filter['mobile_phone'] = empty($_REQUEST['mobile_phone']) ? '' : trim($_REQUEST['mobile_phone']);
    $filter['record_count'] = count($user_count);
    $filter['page_count'] = $filter['record_count'] > 0 ? ceil($filter['record_count'] / $filter['page_size']) : 1;
    
    $result = array();
    $result['list'] = $result_user;
    $result['count'] = count($user_count);
    $result['filter'] = $filter;
    
    return $result;
}

function get_commission_detailed(){
    
    $filter = array();
    $filter['page'] = empty($_REQUEST['page']) || (intval($_REQUEST['page']) <= 0) ? 1 : intval($_REQUEST['page']);
    if (isset($_REQUEST['page_size']) && intval($_REQUEST['page_size']) > 0)
    {
        $filter['page_size'] = intval($_REQUEST['page_size']);
    }
    elseif (isset($_COOKIE['ECSCP']['page_size']) && intval($_COOKIE['ECSCP']['page_size']) > 0)
    {
        $filter['page_size'] = intval($_COOKIE['ECSCP']['page_size']);
    }
    else
    {
        $filter['page_size'] = 15;
    }

    $start = ($filter['page'] - 1) * $filter['page_size'];
    
    $status = $_REQUEST['status'];
    
    //拿到一级分销商用户列表
    $list = array();
    $sql = 'SELECT orders.order_sn,orders.withdraw_status,orders.commissions_ratio,users.user_id,users.user_name,users.mobile_phone,orders.goods_amount,orders.add_time
            FROM ' . $GLOBALS['ecs']->table('order_info') . ' as orders
            left join ' . $GLOBALS['ecs']->table('users') . ' as users on users.user_id = orders.user_id
            WHERE users.parent_uid = '.$_REQUEST['user_id'] ." and users.agent_rank = 2 and orders.withdraw_status = ".$status."
            order by orders.order_id DESC
             limit ".$start.",".$filter['page_size'];;
    $list = $GLOBALS['db']->getAll($sql);
    
    $withdraw_status_array = array("0"=>"未申请","1"=>"申请中","2"=>"已提现");
    //数据处理
    foreach ($list as $k => $v){
        $list[$k]['add_time'] = date("Y-m-d H:i:s",$v['add_time']);
        $list[$k]['withdraw_status'] = $withdraw_status_array[$v['withdraw_status']];
        //提成结算百分比
        $list[$k]['commissions_ratio'] = $status==0?COMMISSIONS_RATIO:$v['commissions_ratio'];
        $list[$k]['commissions_price'] = round($list[$k]['commissions_ratio']*$v['goods_amount'],2);
        $list[$k]['commissions_ratio'] = ($list[$k]['commissions_ratio']*100)."%";
    }

    //再拿一遍总数
    $count = array();
    $sql = 'SELECT orders.order_id
            FROM ' . $GLOBALS['ecs']->table('order_info') . ' as orders
            left join ' . $GLOBALS['ecs']->table('users') . ' as users on users.user_id = orders.user_id
            WHERE users.parent_uid = '.$_REQUEST['user_id'] ." and users.agent_rank = 2 and orders.withdraw_status = ".$status;
    $count = $GLOBALS['db']->getAll($sql);
    
    $filter['status'] = $_REQUEST['status'];
    $filter['user_id'] = $_REQUEST['user_id'];
    $filter['record_count'] = count($count);
    $filter['page_count'] = $filter['record_count'] > 0 ? ceil($filter['record_count'] / $filter['page_size']) : 1;
    
    $result = array();
    $result['list'] = $list;
    $result['count'] = count($count);
    $result['filter'] = $filter;
    $result['status'] = $status;
    $result['user_id'] = $_REQUEST['user_id'];
    
    return $result;
}


?>
